# Maintainer: umarcor <unai.martinezcorral@ehu.eus>

_realname=nextpnr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.3.r0.gd5ec421d
pkgrel=3
pkgdesc="Portable FPGA place and route tool (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/YosysHQ/nextpnr"
license=('ISC')
groups=("${MINGW_PACKAGE_PREFIX}-eda")
depends=(
  "${MINGW_PACKAGE_PREFIX}-boost"
  "${MINGW_PACKAGE_PREFIX}-openmp"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-qt5-base"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-boost"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-clang"
  "${MINGW_PACKAGE_PREFIX}-eigen3"
  "${MINGW_PACKAGE_PREFIX}-icestorm"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-prjtrellis"
  "git"
)

_commit="d5ec421d"
source=("${_realname}::git+https://github.com/YosysHQ/${_realname}.git#commit=${_commit}")
sha256sums=('SKIP')

pkgver() {
  cd "${_realname}"
  git describe --long --tags "${_commit}" | sed 's/nextpnr-//;s/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

build() {
  cd "${srcdir}/${_realname}"
  git submodule update --init --recursive

  unset _extra-archs
  if [ "$CARCH" = "x86_64" ]; then
    _extra_archs=";ecp5"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" cmake . \
    -G "MSYS Makefiles" \
    -DCMAKE_C_COMPILER=clang \
    -DCMAKE_CXX_COMPILER=clang++ \
    -DICESTORM_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_PREFIX_PATH=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DARCH="generic;ice40${_extra_archs}" \
    -DBUILD_HEAP=ON \
    -DUSE_OPENMP=ON \
    -DUSE_IPO=OFF \
    -DBUILD_PYTHON=ON \
    -DBUILD_GUI=ON \
    -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
    -DBUILD_TESTS=ON

  cmake --build . --config Release
}

check() {
  cd "${srcdir}/${_realname}"
  make test
}

package() {
  cd "${srcdir}/${_realname}"
  make DESTDIR="${pkgdir}" install

  _licenses="${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  mkdir -p "${_licenses}"
  install -m 644 "${srcdir}/${_realname}"/COPYING "${_licenses}"
}
